package com.house.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.house.common.annotation.DataScope;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.model.HouseInfo;
import com.house.system.domain.HouseSupport;
import com.house.system.domain.Medium;
import com.house.system.domain.RentalType;
import com.house.system.mapper.*;
import com.house.system.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 房屋信息Service业务层处理
 *
 * @author tc
 * @date 2022-02-04
 */
@Service
public class HouseServiceImpl implements IHouseService {
    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private RentalTypeMapper rentalTypeMapper;

    @Autowired
    private HouseSupportMapper houseSupportMapper;

    @Autowired
    private MediumMapper mediumMapper;

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 查询房屋信息
     *
     * @param houseId 房屋信息主键
     * @return 房屋信息
     */
    @Override
    public House selectHouseByHouseId(Long houseId) {
        return houseMapper.selectHouseByHouseId(houseId);
    }

    /**
     * 查询房屋信息列表
     *
     * @param house 房屋信息
     * @return 房屋信息
     */
    @Override
    @DataScope(tableAlias = "h")
    public List<House> selectHouseList(House house) {
        return houseMapper.selectHouseList(house);
    }

    /**
     * 新增房屋信息
     *
     * @param houseInfo 房屋信息
     * @return 结果
     */
    @Transactional
    @Override
    public Long insertHouse(HouseInfo houseInfo) {
        //添加房屋信息,先添加房屋基本信息
        House house = new House();
        house.setHouseArea(houseInfo.getHouseArea());
        house.setHouseFloor(houseInfo.getHouseFloor());
        house.setHouseFurnished(houseInfo.getHouseFurnished());
        house.setHouseTowards(houseInfo.getHouseTowards());
        house.setHouseName(houseInfo.getHouseName());
        house.setStatus("1");   //发布房源需要审核,先停用
        house.setRentalPrice(houseInfo.getRentalPrice());
        house.setHouseType(houseInfo.getHouseType());
        houseMapper.insertHouse(house);
        //再添加租售类型信息
        RentalType rentalType = new RentalType();
        rentalType.setHouseId(house.getHouseId());
        rentalType.setStatus("0");
        rentalType.setRentalRequest(houseInfo.getRentalRequest());
        rentalType.setPayType(houseInfo.getPayType());
        rentalType.setEntireOrShared(houseInfo.getEntireOrShared());
        rentalType.setRemark(houseInfo.getShareRequest());
        rentalType.setRented(houseInfo.getRented());
        int needed = houseInfo.getNeeded() == 0 ? 1 : houseInfo.getNeeded();
        rentalType.setNeeded(needed);
        rentalTypeMapper.insertRentalType(rentalType);
        //添加房屋配套家具信息
        List<HouseSupport> supports = new ArrayList<>();
        for (String support : houseInfo.getSupports()) {
            HouseSupport houseSupport = new HouseSupport();
            houseSupport.setHouseId(house.getHouseId());
            houseSupport.setSupportName(support);
            supports.add(houseSupport);
        }
        houseSupportMapper.batchAllotSupport(supports);

        //最后添加中介表
        Medium medium = new Medium();
        medium.setUserId(houseInfo.getUserId());
        medium.setHouseId(house.getHouseId());
        mediumMapper.insertMedium(medium);

        return house.getHouseId();
    }

    /**
     * 修改房屋信息
     *
     * @param house 房屋信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHouse(House house) {
        return houseMapper.updateHouse(house);
    }

    /**
     * 删除房屋信息
     *
     * @param houseId 房屋信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseByHouseId(Long houseId) {
        //将与房屋联系的家具信息和租售信息一并删除,同时删除房屋中介表数据
        rentalTypeMapper.deleteRentalTypeByHouseId(houseId);
        houseSupportMapper.deleteHouseSupportByHouseId(houseId);
        mediumMapper.deleteMediumByHouseId(houseId);
        commentMapper.deleteCommentByHouseId(houseId);
        return houseMapper.deleteHouseByHouseId(houseId);
    }

    /**
     * 添加房源图片
     *
     * @param houseId  房屋编号
     * @param houseImg 房源图片
     * @return 结果
     */
    @Override
    public boolean updateHouseImg(Long houseId, String houseImg) {
        House house = new House();
        house.setHouseId(houseId);
        house.setHouseImg(houseImg);
        return houseMapper.updateHouse(house) > 0;
    }
}
